package com.google.project.client.HumanResources;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class HMProjectDetail extends Composite {

	@UiField Label lbProjectCode;
	@UiField Label lbProjectName;
	@UiField Label lbStartDate;
	@UiField Label lbFinishDate;
	@UiField Label lbInvestor;
	@UiField Label lbLocation;
	@UiField Button btnProjectStaffList;
	@UiField TextArea taDescription;
	
	private ProjectDTO currentProject = new ProjectDTO();
	private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	interface Binder extends UiBinder<Widget, HMProjectDetail> {
	}
	private static final Binder binder = GWT.create(Binder.class);
	public HMProjectDetail() {
		initWidget(binder.createAndBindUi(this));
	}
	
	public HMProjectDetail(String projectId) {
		this();
		getProjectInfo(projectId);
		currentProject.setProjectID(projectId);
	}
	
	private void getProjectInfo(String projectId) {
		projectService.getProjectById(projectId, new AsyncCallback<ProjectDTO>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Lỗi trong quá trình tải thông tin!");
			}

			@Override
			public void onSuccess(ProjectDTO result) {
				currentProject = result;
				displayProject();
			}
		});
	}
	
	private void displayProject() {
		lbProjectCode.setText(currentProject.getProjectCode());
		lbProjectName.setText(currentProject.getProjectName());
		lbStartDate.setText(CommonFuntion.convertToDate(new Date(currentProject.getStartDate())));
		lbFinishDate.setText(CommonFuntion.convertToDate(new Date(currentProject.getDueDate())));
		lbLocation.setText(currentProject.getLocation());
		lbInvestor.setText(currentProject.getInvestor());
		taDescription.setText(currentProject.getDescription());
	}

	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		HMEntrypoint.get().HMProjectList();
	}
	@UiHandler("btnProjectStaffList")
	void onBtnProjectStaffListClick(ClickEvent event) {
		HMEntrypoint.get().HMProjectStaffList(currentProject);
	}
}
